package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.MenuService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rbac/menu")
@Menu("权限管理系统.菜单管理")
public class MenuController {
	
	@Autowired
	MenuService  menuService;
	
	/**
	 * 分页查询菜单列表
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询菜单列表")
	public Result page(@RequestBody Model params){
		Page page=menuService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	/**
	 * 新增菜单
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增菜单")
	public Result add(@RequestBody Model params){
		if(menuService.add(params)){
			return Result.Success("RBAC_MENU_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_MENU_ADD_ERROR");
		}
	}
	
	/**
	 * 新增菜单
	 * @param request
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取菜单详情")
	public Result get(String id){
		return Result.Success(menuService.get(id),"");
	}
	
	/**
	 * 更新菜单
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新菜单")
	public Result update(@RequestBody Model params){
		if(menuService.update(params)){
			return Result.Success("RBAC_MENU_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_MENU_UPDATE_ERROR");
		}		
	}
	
	/**
	 * 删除菜单
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除菜单")
	public Result delete(String id){
		if(menuService.delete(id)){
			return Result.Success("RBAC_MENU_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_MENU_DELETE_ERROR");
		}
	}
	
	/**
	 * 启用禁用
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用")
	public Result checkState(String id){
		if(menuService.checkState(id)){
			return Result.Success("RBAC_MENU_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_MENU_CHECKSTATE_ERROR");
		}
	}

	/**
	 * 升序降序
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String id,int type){
		if(menuService.sort(id, type)){
			return Result.Success("RBAC_MENU_SORT_SUCCESS");
		}else{
			return Result.Error("RBAC_MENU_SORT_ERROR");
		}
	}
	
	@GetMapping("childs")
	@Permission("查询子菜单")
	public Result childs(String sysId,String parentId){
		return Result.Success(menuService.childs(sysId,parentId));
	}
	
	@GetMapping("tree")
	@Permission("加载所有系统和菜单的树形结构数据")
	public Result tree(){
		return Result.Success(menuService.tree());
	}
}
